﻿using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
/*
FilterType 说明 备注
    -4 在 DXF 编码中使用值“-4”来指示过滤器规格中的关系运算符和逻辑运算符
    0 图形对象类型 如“LWPOLYLINE”、“3DFACE”、“POINT”、“LINE”、“TEXT”、“MTEXT”、“CIRCLE”、“ARC”、“POLYLINE”、“3DSOLID”、“REGION”、“SPLINE”等
    8 图层名称
    40 是根据 DXF 组码查询得知圆的半径所对应的组码值为 40
    60 对象可见性
    62 颜色编号 范围 0 到 256 内的数字索引值。零表示 BYBLOCK，256 表示 BYLAYER，负值表示图层被关
    */
namespace CAD开发
{
    public partial class Form1 : Form
    {
        private Autodesk.AutoCAD.Interop.AcadApplication AcadApp;
        private Autodesk.AutoCAD.Interop.AcadDocument AcadDoc;
        private double[] pickPointArray;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            启动CAD();
            刷新图层显示();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog pOpenDG = new OpenFileDialog();
            pOpenDG.ShowDialog();
            string filePath = pOpenDG.FileName;
            AcadApp = new AcadApplication();
            AcadApp.Application.Visible = true;
            AcadDoc = AcadApp.Documents.Open(filePath, null, null);
        }

        private void 启动CAD()
        {
            try
            {
                AcadApp =
               (AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application");
                AcadDoc = AcadApp.ActiveDocument;
            }
            catch
            {
                OpenFileDialog pOpenDG = new OpenFileDialog();
                pOpenDG.Filter = "CAD文件(*.dwg)|*.dwg|CAD图形文件(*.dxf)|*.dxf";
                pOpenDG.Title = "打开CAD文件";
                pOpenDG.ShowDialog();
                string filePath = pOpenDG.FileName;
                if (filePath == "")
                {
                    MessageBox.Show("选择CAD文件无效！", "文件无效！");
                    Application.Exit();
                }
                AcadApp = new AcadApplication();
                AcadDoc = AcadApp.Documents.Open(filePath, null, null);
            }
            AcadApp.Application.Visible = true;
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
        }

        private void 获取对象名称_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取物体对象!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            MessageBox.Show("选取对象为：" + returnCADObj.ObjectName.ToString());

            pickPointArray = (Double[])pickPoint;
            MessageBox.Show("点击位置：X=" + pickPointArray[0].ToString() + ",Y=" +
pickPointArray[1].ToString() + ",Z=" + pickPointArray[2].ToString());

            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void 获取直线的坐标_Click(object sender, EventArgs e)
        {
            handle01:
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取直线!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            if (returnCADObj.ObjectName == "AcDbLine")
            {
                AcadLine returnLine = (AcadLine)returnCADObj;
                double[] lineStartPoint = new Double[2];
                double[] lineEndPoint = new Double[2];
                lineStartPoint = (Double[])returnLine.StartPoint;
                lineEndPoint = (Double[])returnLine.EndPoint;
                MessageBox.Show("直线的起点坐标为：" + lineStartPoint[0].ToString()
                + "," + lineStartPoint[1].ToString()
                + "," + lineStartPoint[2].ToString()
                + "\n" + "直线的终点坐标为：" + lineEndPoint[0].ToString()
                + "," + lineEndPoint[1].ToString()
                + "," + lineEndPoint[2].ToString());
            }
            else
            {
                MessageBox.Show("选择的不是直线！请重新选择！");
                goto handle01;
            }
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void 获取多段线节点坐标_Click(object sender, EventArgs e)
        {
            handle01:
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取多段线!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            if (returnCADObj.ObjectName == "AcDbPolyline")
            {
                AcadLWPolyline returnPolyLine = (AcadLWPolyline)returnCADObj;
                double[] polyLinePoint;
                polyLinePoint = (Double[])returnPolyLine.Coordinates;
                int i, pointCount = polyLinePoint.Length / 2;
                string showInfo = "";
                for (i = 0; i < polyLinePoint.Length - 1; i = i + 2)
                {
                    showInfo += polyLinePoint[i].ToString() + "," + polyLinePoint[i + 1].ToString() +
                   "\n";
                }
                showInfo += "标高为：" + returnPolyLine.Elevation.ToString();
                ResultInfo.Text = showInfo;
            }
            else
            {
                MessageBox.Show("选择的不是多段线！请重新选择！");
                goto handle01;
            }
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);        
        }

        private void 获取三维多段线节点坐标_Click(object sender, EventArgs e)
        {
            handle01:
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取三维多段线!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            if (returnCADObj.ObjectName == "AcDb3dPolyline")
            {
                Acad3DPolyline return3dPolyLine = (Acad3DPolyline)returnCADObj;
                double[] TDpolyLinePoint;
                TDpolyLinePoint = (Double[])return3dPolyLine.Coordinates;
                int i;
                string showInfo = "";
                for (i = 0; i < TDpolyLinePoint.Length - 2; i = i + 3)
                {
                    showInfo += TDpolyLinePoint[i].ToString() + "," + TDpolyLinePoint[i +
                   1].ToString()
                    + "," + TDpolyLinePoint[i + 2].ToString() + "\n";
                }
                ResultInfo.Text = showInfo;
            }
            else
            {
                MessageBox.Show("选择的不是三维多段线！请重新选择！");
                goto handle01;
            }
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void 获取点的坐标_Click(object sender, EventArgs e)
        {
            handle01:
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取点!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            if (returnCADObj.ObjectName == "AcDbPoint")
            {
                AcadPoint returnPointObj = (AcadPoint)returnCADObj;
                double[] PointCoord;
                PointCoord = (Double[])returnPointObj.Coordinates;
                string showInfo;
                showInfo = PointCoord[0].ToString() + "," + PointCoord[1].ToString()
                + "," + PointCoord[2].ToString();
                ResultInfo.Text = showInfo;
            }
            else
            {
                MessageBox.Show("选择的不是点！请重新选择！");
                goto handle01;
            }
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void 获取块的插入点_Click(object sender, EventArgs e)
        {
            handle01:
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取块!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            if (returnCADObj.ObjectName == "AcDbBlockReference")
            {
                AcadBlockReference returnBlock = (AcadBlockReference)returnCADObj;
                double[] blockPoint;
                blockPoint = (Double[])returnBlock.InsertionPoint;
                string showInfo;
                showInfo = blockPoint[0].ToString() + "," + blockPoint[1].ToString()
                + "," + blockPoint[2].ToString();
                ResultInfo.Text = showInfo;
            }
            else
            {
                MessageBox.Show("选择的不是块！请重新选择！");
                goto handle01;
            }
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void 获取文字插入点及内容_Click(object sender, EventArgs e)
        {
            handle01:
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取文字!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            if (returnCADObj.ObjectName == "AcDbText")
            {
                AcadText returnTextObj = (AcadText)returnCADObj;
                double[] textInsertionPoint;
                textInsertionPoint = (Double[])returnTextObj.InsertionPoint;
                string showInfo;
                showInfo = textInsertionPoint[0].ToString() + "," + textInsertionPoint[1].ToString()
                + "," + textInsertionPoint[2].ToString();
                showInfo += "\n文字内容为：" + returnTextObj.TextString.ToString();
                ResultInfo.Text = showInfo;
            }
            else if (returnCADObj.ObjectName == "AcDbMText")
            {
                AcadMText returnMTextObj = (AcadMText)returnCADObj;
                double[] textInsertionPoint;
                textInsertionPoint = (Double[])returnMTextObj.InsertionPoint;
                string showInfo;
                showInfo = textInsertionPoint[0].ToString() + "," + textInsertionPoint[1].ToString()
                + "," + textInsertionPoint[2].ToString();
                showInfo += "\n文字内容为：" + returnMTextObj.TextString.ToString();
                ResultInfo.Text = showInfo;
            }
            else
            {
                MessageBox.Show("选择的不是文字！请重新选择！");
                goto handle01;
            }
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void 获取样条曲线控制点坐标_Click(object sender, EventArgs e)
        {
            handle01:
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取样条曲线!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            if (returnCADObj.ObjectName == "AcDbSpline")
            {
                AcadSpline returnSpline = (AcadSpline)returnCADObj;
                double[] splineControlPoint;
                splineControlPoint = (Double[])returnSpline.ControlPoints;
                string showInfo = "";
                int i;
                for (i = 0; i < splineControlPoint.Length - 2; i = i + 3)
                {
                    showInfo += splineControlPoint[i].ToString() + "," + splineControlPoint[i +
                   1].ToString()
                    + "," + splineControlPoint[i + 2].ToString() + "\n";
                }
                ResultInfo.Text = showInfo;
            }
            else
            {
                MessageBox.Show("选择的不是样条曲线！请重新选择！");
                goto handle01;
            }
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void 获取样条曲线拟合点坐标_Click(object sender, EventArgs e)
        {
            handle01:
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取样条曲线!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            if (returnCADObj.ObjectName == "AcDbSpline")
            {
                AcadSpline returnSpline = (AcadSpline)returnCADObj;
                double[] splineFitPoint;
                splineFitPoint = (Double[])returnSpline.FitPoints;
                string showInfo = "";
                int i;
                for (i = 0; i < splineFitPoint.Length - 2; i = i + 3)
                {
                    showInfo += splineFitPoint[i].ToString() + "," + splineFitPoint[i + 1].ToString()
                    + "," + splineFitPoint[i + 2].ToString() + "\n";
                }
                ResultInfo.Text = showInfo;
            }
            else
            {
                MessageBox.Show("选择的不是样条曲线！请重新选择！");
                goto handle01;
            }
            Microsoft.VisualBasic.Interaction.AppActivate(this.Text);
        }

        private void 在屏幕上选择对象_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet01");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";
            mySelectionSet.SelectOnScreen(FilterType, FilterData);
            MessageBox.Show("共选择对象个数为：" + mySelectionSet.Count.ToString());
            AcadDoc.SelectionSets.Item("NewSelectionSet01").Delete();
        }

        private void 选择全部对象_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet02");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";
            double[] point01 = new double[3];
            double[] point02 = new double[3];
            point01[0] = 0; point01[1] = 0; point01[2] = 0;
            point02[0] = 1000; point02[1] = 1000; point02[2] = 0;
            mySelectionSet.Select(AcSelect.acSelectionSetAll, point01, point02, FilterType,
           FilterData);
            MessageBox.Show("共选择对象个数为：" + mySelectionSet.Count.ToString());
            AcadDoc.SelectionSets.Item("NewSelectionSet02").Delete();
        }

        private void 刷新图层显示()
        {
            LayerComboBox.Items.Clear();
            int i;
            for (i = 0; i < AcadDoc.Layers.Count; i++)
            {
                LayerComboBox.Items.Add(AcadDoc.Layers.Item(i).Name.ToString());
            }
            LayerComboBox.Text = AcadDoc.ActiveLayer.Name.ToString();
        }

        private void 选择指定图层中对象_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet03");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 8;
            FilterData[0] = LayerComboBox.Text;
            double[] point01 = new double[3];
            double[] point02 = new double[3];
            point01[0] = 0; point01[1] = 0; point01[2] = 0;
            point02[0] = 1000; point02[1] = 1000; point02[2] = 0;
            mySelectionSet.Select(AcSelect.acSelectionSetAll, point01, point02, FilterType,
           FilterData);
            MessageBox.Show("共选择对象个数为：" + mySelectionSet.Count.ToString());
            AcadDoc.SelectionSets.Item("NewSelectionSet03").Delete();
        }

        private void 选择指定图形对象_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet04");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "LWPOLYLINE";
            double[] point01 = new double[3];
            double[] point02 = new double[3];
            point01[0] = 0; point01[1] = 0; point01[2] = 0;
            point02[0] = 1000; point02[1] = 1000; point02[2] = 0;
            mySelectionSet.Select(AcSelect.acSelectionSetAll, point01, point02, FilterType, FilterData);
            MessageBox.Show("共选择多段线个数为：" + mySelectionSet.Count.ToString());
            AcadDoc.SelectionSets.Item("NewSelectionSet04").Delete();
        }

        private void 多条件选择_Click(object sender, EventArgs e)
        {
            /*Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet05");
            Int16[] FilterType = new Int16[2];
            object[] FilterData = new object[2];
            FilterType[0] = 0;
            FilterData[0] = "LINE";
            FilterType[1] = 8;
            FilterData[1] = "0";
            double[] point01 = new double[3];
            double[] point02 = new double[3];
            point01[0] = 0; point01[1] = 0; point01[2] = 0;
            point02[0] = 1000; point02[1] = 1000; point02[2] = 0;
            mySelectionSet.Select(AcSelect.acSelectionSetAll, point01, point02, FilterType,
           FilterData);
            MessageBox.Show("共选择\"0\"图层中直线个数为：" + mySelectionSet.Count.ToString());
            AcadDoc.SelectionSets.Item("NewSelectionSet05").Delete();*/
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet05");
            Int16[] FilterType = new Int16[8];
            object[] FilterData = new object[8];
            FilterType[0] = 0;
            FilterData[0] = "Circle";
            FilterType[1] = 8;
            FilterData[1] = "0";
            FilterType[2] = -4;
            FilterData[2] = "<AND";
            FilterType[3] = -4;
            FilterData[3] = ">=";
            FilterType[4] = 40;
            FilterData[4] = 10;
            FilterType[5] = -4;
            FilterData[5] = "<=";
            FilterType[6] = 40;
            FilterData[6] = 100;
            FilterType[7] = -4;
            FilterData[7] = "AND>";
            double[] point01 = new double[3];
            double[] point02 = new double[3];
            point01[0] = 0; point01[1] = 0; point01[2] = 0;
            point02[0] = 1000; point02[1] = 1000; point02[2] = 0;
            mySelectionSet.Select(AcSelect.acSelectionSetAll, point01, point02, FilterType,
FilterData);
            MessageBox.Show("共选择满足条件的圆个数为：" + mySelectionSet.Count.ToString());
            AcadDoc.SelectionSets.Item("NewSelectionSet05").Delete();
        }

        private void 根据范围选择对象_Click(object sender, EventArgs e)
        {
            handle01:
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取Polyline对象!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            if (returnCADObj.ObjectName != "AcDbPolyline") goto handle01;
            AcadLWPolyline returnLWPolyline = (AcadLWPolyline)returnCADObj;
            if (!returnLWPolyline.Closed)
            {
                MessageBox.Show("选取的多段线没有闭合！");
                goto handle01;
            }
            Double[] selectionEdgePoints, LWPolylineCoords;
            LWPolylineCoords = (Double[])returnLWPolyline.Coordinates;
            int i;
            selectionEdgePoints = new Double[3 * LWPolylineCoords.Length / 2];
            for (i = 0; i < LWPolylineCoords.Length / 2; i++)
            {
                selectionEdgePoints[3 * i] = LWPolylineCoords[2 * i];
                selectionEdgePoints[3 * i + 1] = LWPolylineCoords[2 * i + 1];
                selectionEdgePoints[3 * i + 2] = 0;
            }

            AcadSelectionSet mySelectionSet;
            mySelectionSet = AcadDoc.SelectionSets.Add("NewSelectionSet06");
            Int16[] FilterType = new Int16[1];
            object[] FilterData = new object[1];
            FilterType[0] = 0;
            FilterData[0] = "*";

            mySelectionSet.SelectByPolygon(AcSelect.acSelectionSetCrossingPolygon, selectionEdgePoints,
            FilterType, FilterData);
            MessageBox.Show("范围内图形对象个数为：" + mySelectionSet.Count.ToString());
            AcadDoc.SelectionSets.Item("NewSelectionSet06").Delete();
        }

        private void 绘制直线_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            double[] startPoint = new double[3];
            double[] endPoint = new double[3];
            startPoint[0] = 0; startPoint[1] = 0; startPoint[2] = 0;
            endPoint[0] = 100; endPoint[1] = 100; endPoint[2] = 0;
            AcadDoc.ModelSpace.AddLine(startPoint, endPoint);
        }

        private void 添加文字_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            string textString = "Hello";
            double[] textInsertPoint = new double[3];
            double textHeight = 2.0;
            textInsertPoint[0] = 0; textInsertPoint[1] = 0; textInsertPoint[2] = 0;
            AcadDoc.ModelSpace.AddText(textString, textInsertPoint, textHeight);
        }

        private void 绘制三维多段线_Click(object sender, EventArgs e)
        {
            handle01:
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取Polyline对象!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            if (returnCADObj.ObjectName != "AcDbPolyline") goto handle01;
            AcadLWPolyline returnLWPolyline = (AcadLWPolyline)returnCADObj;
            if (!returnLWPolyline.Closed)
            {
                MessageBox.Show("选取的多段线没有闭合！");
                goto handle01;
            }
            Double[] LWPolylineCoords, create3DPolyCoords;
            LWPolylineCoords = (Double[])returnLWPolyline.Coordinates;
            int i;
            create3DPolyCoords = new Double[3 * LWPolylineCoords.Length / 2];
            for (i = 0; i < LWPolylineCoords.Length / 2; i++)
            {
                create3DPolyCoords[3 * i] = LWPolylineCoords[2 * i];
                create3DPolyCoords[3 * i + 1] = LWPolylineCoords[2 * i + 1];
                create3DPolyCoords[3 * i + 2] = 0;
            }
            AcadDoc.ModelSpace.Add3DPoly(create3DPolyCoords);
        }

        private void 复制选中对象_Click(object sender, EventArgs e)
        {
            handle01:
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取Polyline对象!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            if (returnCADObj.ObjectName != "AcDbPolyline") goto handle01;
            AcadLWPolyline returnLWPolyline = (AcadLWPolyline)returnCADObj;
            returnLWPolyline.Copy();
        }

        private void 删除选中对象_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取对象!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            returnCADObj.Delete();
        }

        private void 旋转多段线_Click(object sender, EventArgs e)
        {
            handle01:
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取Polyline对象!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            if (returnCADObj.ObjectName != "AcDbPolyline") goto handle01;
            AcadLWPolyline returnLWPolyline = (AcadLWPolyline)returnCADObj;
            double[] rotateBase = new double[3];
            double[] lwPolylineCoord;
            lwPolylineCoord = (double[])returnLWPolyline.Coordinates;
            rotateBase[0] = lwPolylineCoord[0];
            rotateBase[1] = lwPolylineCoord[1];
            rotateBase[2] = lwPolylineCoord[2];
            double rotateAngle = 90.0 * Math.PI / 180;
            returnLWPolyline.Rotate(rotateBase, rotateAngle);
        }

        private void 缩放多段线_Click(object sender, EventArgs e)
        {
            handle01:
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取Polyline对象!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            if (returnCADObj.ObjectName != "AcDbPolyline") goto handle01;
            AcadLWPolyline returnLWPolyline = (AcadLWPolyline)returnCADObj;
            double[] scaleBase = new double[3];
            double[] lwPolylineCoord;
            lwPolylineCoord = (double[])returnLWPolyline.Coordinates;
            scaleBase[0] = lwPolylineCoord[0];
            scaleBase[1] = lwPolylineCoord[1];
            scaleBase[2] = lwPolylineCoord[2];
            double scaleFactor = 5.0f;
            returnLWPolyline.ScaleEntity(scaleBase, scaleFactor);
        }

        private void 移动多段线_Click(object sender, EventArgs e)
        {
            handle01:
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取Polyline对象!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            if (returnCADObj.ObjectName != "AcDbPolyline") goto handle01;
            AcadLWPolyline returnLWPolyline = (AcadLWPolyline)returnCADObj;
            double[] fromPoint = new double[3];
            double[] toPoint = new double[3];
            fromPoint[0] = 0; fromPoint[1] = 0; fromPoint[2] = 0;
            toPoint[0] = 500; toPoint[1] = 500; toPoint[2] = 0;
            returnLWPolyline.Move(fromPoint, toPoint);
        }

        private void 多段线转化成面域_Click(object sender, EventArgs e)
        {
            handle01:
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            object returnObj, pickPoint;
            string pickPrompt = "选取Polyline对象!";
            AcadDoc.Utility.GetEntity(out returnObj, out pickPoint, pickPrompt);
            AcadObject returnCADObj = (AcadObject)returnObj;
            if (returnCADObj.ObjectName != "AcDbPolyline") goto handle01;
            AcadLWPolyline returnLWPolyline = (AcadLWPolyline)returnCADObj;
            if (!returnLWPolyline.Closed)
            {
                MessageBox.Show("选取的多段线没有闭合！");
                goto handle01;
            }
            AcadLWPolyline[] lWPolylineObj = new AcadLWPolyline[1];
            lWPolylineObj[0] = returnLWPolyline;
            lWPolylineObj[0].Closed = true;
            AcadDoc.ModelSpace.AddRegion(lWPolylineObj);
            returnLWPolyline.Delete();
        }

        private void 添加新的图层_Click(object sender, EventArgs e)
        {
            handle01:
            string newLayerName;
            newLayerName = Microsoft.VisualBasic.Interaction.InputBox("请输入图层的名称！", "创建新图层","",500,400); 
           
            int i;
            for (i = 0; i < AcadDoc.Layers.Count; i++)
            {
                if (AcadDoc.Layers.Item(i).Name == newLayerName)
                {
                    MessageBox.Show("图层已经存在！请输入新的名称！");
                    goto handle01;
                }
            }
            AcadDoc.Layers.Add(newLayerName);
        }

        private void 删除指定图层_Click(object sender, EventArgs e)
        {
            try
            {
                AcadDoc.Layers.Item(LayerComboBox.Text).Delete();
            }
            catch
            {
                MessageBox.Show("选择删除的图层为当前图层、图层0、依赖于外部参照的图层或包含对象的图层！不能删除！"); 
           }
        }

        private void 向CAD发送命令_Click(object sender, EventArgs e)
        {
            Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption);
            AcadDoc.SendCommand("_point\n100,100,0\n");
        }

        private void 创建一个新文件_Click(object sender, EventArgs e)
        {
            AcadApp.Documents.Add("");
        }

        private void 保存CAD文件_Click(object sender, EventArgs e)
        {
            if (AcadApp.ActiveDocument.Saved == false)
            {
                AcadApp.ActiveDocument.Save();
            }
        }

        private void 文件另存为_Click(object sender, EventArgs e)
        {
            handle01:
            SaveFileDialog savefileDG = new SaveFileDialog();
            savefileDG.Filter = "AutoCAD 2007 DWG文件(*.dwg)|*.dwg" + "|AutoCAD 2007 DXF文件(*.dxf)|*.dxf"
 + "|AutoCAD 2004 DWG文件(*.dwg)|*.dwg"
 + "|AutoCAD 2004 DXF文件(*.dxf)|*.dxf"
 + "|AutoCAD 2000 DWG文件(*.dwg)|*.dwg"
 + "|AutoCAD 2000 DXF文件(*.dxf)|*.dxf";
            savefileDG.Title = "CAD文件另存为";
            savefileDG.ShowDialog();
            string savePath = savefileDG.FileName;
            if (savePath == "")
            {
                MessageBox.Show("文件保存路径无效！");
                goto handle01;
            }
            switch (savefileDG.FilterIndex)
            {
                case 1:
                    AcadApp.ActiveDocument.SaveAs(savePath, AcSaveAsType.ac2007_dwg, null);
                    break;
                case 2:
                    AcadApp.ActiveDocument.SaveAs(savePath, AcSaveAsType.ac2007_dxf, null);
                    break;
                case 3:
                    AcadApp.ActiveDocument.SaveAs(savePath, AcSaveAsType.ac2004_dwg, null);
                    break;
                case 4:
                    AcadApp.ActiveDocument.SaveAs(savePath, AcSaveAsType.ac2004_dxf, null);
                    break;
                case 5:
                    AcadApp.ActiveDocument.SaveAs(savePath, AcSaveAsType.ac2000_dwg, null);
                    break;
                case 6:
                    AcadApp.ActiveDocument.SaveAs(savePath, AcSaveAsType.ac2000_dxf, null);
                    break;
            }
        }

        private void 关闭CAD文件_Click(object sender, EventArgs e)
        {
            if (AcadDoc.Saved == false)
            {
                DialogResult selectResult = MessageBox.Show("是否保存更改？", "关闭CAD文档",
               MessageBoxButtons.YesNoCancel);
                switch (selectResult.ToString())
                {
                    case "Yes":
                        AcadDoc.Close("TRUE", AcadDoc.FullName);
                        break;
                    case "No":
                        AcadDoc.Close("FALSE", AcadDoc.FullName);
                        break;
                    case "Cancel":
                        break;
                }
            }
            else
            {
                AcadDoc.Close("FALSE", AcadDoc.FullName);
            }
        }

        private void 退出CAD_Click(object sender, EventArgs e)
        {
            AcadApp.Quit();
        }

    }
}
